<template>
	<div class="book-detail">
		<el-container>
			<el-aside width="200px">
				<div class="typeItems" @click="getBookAllBook, getAllid(1000)" :class="{ bgcolor: bgid === 1000 }">全部商品</div>
				<div class="typeItems" v-for="item in typeList" :key="item.id" @click="getTypeBook(item.id), getBg(item.id)"
					:class="{ bgcolor: bgid === Number(item.id) }">
					{{ item.type }}
				</div>
			</el-aside>
			<el-main v-loading="isLoading" element-loading-text="Loading...">
				<div class="book-content">
					<div class="book-items" v-for="items in itemsList" :key="items.id">
						<div class="left-image">
							<el-image :src="`${$Imgurl}/api/public/showImg/${items.picture}`" lazy></el-image>
						</div>
						<div class="right-content">
							<h3>{{ items.name }}</h3>
							<p>作者：{{ items.author }}</p>
							<p>出版社：{{ items.publisher }}</p>
							<p>￥{{ items.price }}</p>
							<p><button @click="addCart(items.id)">购买</button></p>
						</div>
					</div>
				</div>
			</el-main>
		</el-container>
		<!-- 分页 -->
		<div class="book-pagination">
			<el-pagination background layout="prev, pager, next" :total="total" :page-size='num' @current-change='divPages'>
			</el-pagination>
		</div>
	</div>
</template>

<script>
import { $post } from "@/ajax";
export default {
	name: '',
	data () {
		return {
			typeList: [],
			itemsList: [],
			isLoading: true,
			total: 0,
			num: 8,
			page: 0,
			bgid: 1000
		}
	},
	methods: {

		// 点击切换背景颜色
		getBg (id) {
			this.bgid = Number(id);
		},

		getAllid (id) {
			this.bgid = Number(id);
		},

		async getTypeList (url) {
			const resultTypeList = await this.axios.get(url);
			this.typeList = resultTypeList.data.data;
		},

		async getBookAllBook () {

			const resultBookItems = await this.axios.get('/api/public/findBookByPage/0/8/0');
			this.itemsList = resultBookItems.data.data.books;
			this.total = resultBookItems.data.data.total;
			if (resultBookItems.data.code === 200) {
				this.isLoading = false;
			}
		},

		async getTypeBook (id) {
			const resultTypeBook = await this.axios.get('/api/public/findBookByPage/0/8/' + id);
			this.itemsList = resultTypeBook.data.data.books;
			this.total = resultTypeBook.data.data.total;
			if (resultTypeBook.data.code === 200) {
				this.isLoading = false;
			}
		},

		async divPages (x) {
			let pages = `/api/public/findBookByPage/${x - 1}/8/0`;
			const result = await this.axios.get(pages);
			this.itemsList = result.data.data.books;
			if (result.data.code === 200) {
				this.isLoading = false;
			}
		},

		// 添加购物车
		async addCart (id) {
			const token = localStorage.getItem('token');
			// 如果token 为空,直接return
			if (!token) {
				this.$message({
					message: '请先登录',
					type: 'warning',
					duration: 1500,
				});
				return;
			}

			const resultBuy = await $post(`/api/public/addCart/${id}`);

			if (resultBuy.status === 200) {
				this.$message({
					message: '添加成功',
					type: 'success'
				});
			} else {
				this.$message({
					message: '添加失败',
					type: 'error'
				});
			}

		}



	},
	mounted () { },
	created () {
		this.getTypeList('/api/public/type/findAll');
		this.getBookAllBook();
	}
}
</script>

<style scoped>
/* 全局样式 */
* {
	margin: 0;
	padding: 0;
	list-style: none;
}

/* 去除的滚动条 */
.el-aside {
	overflow: hidden;
}

/* el-aside添加边框 占满整个可视区域 */
.el-aside {
	border-right: 1px solid #ccc;
	height: calc(100vh - 105px);

}

/* 侧边栏样式 */
.el-aside div {
	height: 50px;
	line-height: 50px;
	text-align: center;
	border-bottom: 1px solid #ccc;
}

/* 主体样式 */
.el-main {
	height: 100%;
}

.typeItems {
	background-color: white;
	color: black;
}

.typeItems:hover {
	cursor: pointer;
}

.book-content {
	width: 100%;
	max-height: 530px;
	display: flex;
	flex-wrap: wrap;
}


.book-items {
	width: 45%;
	height: 100%;
	display: flex;
	margin: 10px 20px;
	border-radius: 5px;
	border: 1px solid gainsboro;
	padding: 10px 0;
}

.book-items:hover {
	transform: scale(1.1);
	transition: all 0.5s;
}

.left-image {
	width: 30%;
	height: 100%;
}


.right-content {
	width: 65%;
	margin: 0 auto;
	height: 100%;
	display: flex;
	flex-direction: column;
	justify-content: space-around;
	line-height: 35px;
}

.right-content h3 {
	font-size: 20px;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.right-content p {
	font-size: 16px;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.right-content button {
	width: 100px;
	height: 30px;
	background-color: rgb(245, 108, 108);
	color: white;
	border-radius: 5px;
	border: none;
}

.right-content button:hover {
	cursor: pointer;
	background-color: rgb(248, 152, 152);
}

.right-content button:focus {
	outline: none;
}

.right-content button:active {
	transform: scale(0.9);
}

.book-pagination {
	margin-top: 10px;
	text-align: center;
}

.bgcolor {
	background-color: dodgerblue !important;
	color: white !important;
	font-weight: bold !important;
}
</style>

